import { effect } from './effect';

// 计算属性 - 函数式
export const computed = (getter: Function) => {
  let catchValue;
  const _value = effect(getter, {
    scheduler: () => _dirty = true
  })

  let _dirty = true

  class ComputedRefImpl {
    get value() {
      if (_dirty) { // 重新计算
        catchValue = _value()
        _dirty = false
      }

      return catchValue // 缓存值
    }
  }

  return new ComputedRefImpl()
}